package com.smartkey.activity;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.project.smartkey.R;
import com.smartkey.bean.AppInfo;
import com.smartkey.sharedpreferences.SharePreferencesUtil;
import com.smartkey.utils.AppUtil;
import com.smartkey.utils.ClickType;

/**
 * 短信设置对话框
 *
 */
public class SMSSettingActivity extends BaseActivity{
	private EditText edt_phone;
	private EditText edt_msg;
	private ImageView img_contact;
	private ImageView img_ok;
	private ImageView img_cancel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
	}
	
	private void initView() {
		setContentView(R.layout.sms_setting_dialog_layout);
		edt_phone = (EditText) findViewById(R.id.edt_phone);
		edt_msg = (EditText) findViewById(R.id.edt_msg);
		
		img_contact = (ImageView) findViewById(R.id.img_contact);
		img_ok = (ImageView) findViewById(R.id.img_ok);
		img_cancel = (ImageView) findViewById(R.id.img_cancel);
		
		//选择联系人
		img_contact.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(  
		                 Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI), 0);
			}
		});
		//确定
		img_ok.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				String num = edt_phone.getText().toString().trim();
				String msg = edt_msg.getText().toString().trim();
				if(num.equals("")){
					Toast.makeText(SMSSettingActivity.this, "请输入电话号码", Toast.LENGTH_SHORT).show();
					return;
				}
				if(msg.equals("")){
					Toast.makeText(SMSSettingActivity.this, "请输入短信内容", Toast.LENGTH_SHORT).show();
					return;
				}
				
				SharePreferencesUtil sharePreferencesUtil = new SharePreferencesUtil(
						SMSSettingActivity.this);
				sharePreferencesUtil.setSMS(num, msg);
				
				Intent aintent = new Intent(SMSSettingActivity.this, AppPickWithDefaultActivty.class);
				setResult(101,aintent); 
				finish();
			}
		});
		//取消
		img_cancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				finish();
			}
		});
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	super.onActivityResult(requestCode, resultCode, data);
	if (resultCode == Activity.RESULT_OK) { 
	//ContentProvider展示数据类似一个单个数据库表
	//ContentResolver实例带的方法可实现找到指定的ContentProvider并获取到ContentProvider的数据
	            ContentResolver reContentResolverol = getContentResolver();  
	            //URI,每个ContentProvider定义一个唯一的公开的URI,用于指定到它的数据集
	            Uri contactData = data.getData();
	            //查询就是输入URI等参数,其中URI是必须的,其他是可选的,如果系统能找到URI对应的ContentProvider将返回一个Cursor对象.
	            Cursor cursor = managedQuery(contactData, null, null, null, null);  
	            cursor.moveToFirst(); 
//	            //获得DATA表中的名字
//	            username = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));  
	            //条件为联系人ID
	            String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
	            // 获得DATA表中的电话号码，条件为联系人ID,因为手机号码可能会有多个
	            Cursor phone = reContentResolverol.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,   
	                     null,   
	                     ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,   
	                     null,   
	                     null);  
	             while (phone.moveToNext()) {  
	                String usernumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));  
//	                 et_mobile.setText(usernumber+" ("+username+")");  
	            	 System.out.println("########## phoneNumber = "+usernumber);
	            	 edt_phone.setText(usernumber);
	             }  
	  
	        }  
	}
}
